package com.slider.sun;

import android.content.DialogInterface;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 */
public class FullscreenActivity extends AppCompatActivity {
    private  int type;
    private ImageView imageView;
    private ImageView iv_anim1;
    private ImageView iv_anim2;
    private ImageView iv_anim3;
    private Animation animation;
    private MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fullscreen);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        type = SharedPreferencesUtils.getInt(this, "type", -1);
        imageView = findViewById(R.id.image);
        iv_anim1 = findViewById(R.id.image_anim1);
        iv_anim2 = findViewById(R.id.image_anim2);
        iv_anim3 = findViewById(R.id.image_anim3);
        startanim(iv_anim1,true,1000);
        startanim(iv_anim2,false,1000);
        startanim(iv_anim3,true,1000);
        mediaPlayer = new MediaPlayer();
//        findViewById(R.id.rl_anim).setOnLongClickListener(new View.OnLongClickListener() {
//            @Override
//            public boolean onLongClick(View view) {
//                startanim(iv_anim1,true,2000);
//                startanim(iv_anim2,false,2000);
//                startanim(iv_anim3,true,2000);
//                return false;
//            }
//        });


        if (type<0) {
            showSingleChoiceDialog();
        } else {
//            if(type<=1){
                setlistener();
//            }
            imageView.setImageResource(images[type]);
        }
    }

    private void setlistener() {
        findViewById(R.id.btn_test).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.e("TAG",event.getAction()+"event.getAction()");
                if(v.getId() == R.id.btn_test){
                    if(event.getAction() == MotionEvent.ACTION_UP){
                        Log.d("test", "cansal button ---> cancel");
                        startanim(iv_anim1,true,1000);
                        startanim(iv_anim2,false,1000);
                        startanim(iv_anim3,true,1000);
                    }
                    if(event.getAction() == MotionEvent.ACTION_DOWN){
                        Log.d("test", "cansal button ---> down");
                        startanim(iv_anim1,true,2000);
                        startanim(iv_anim2,false,2000);
                        startanim(iv_anim3,true,2000);
//                        AVChatSoundPlayer.instance(FullscreenActivity.this).playbyid(raws[type]);
                        mediaPlayer.release();
                        mediaPlayer=new MediaPlayer();
                        SoundUtils.playSoundByMedia(mediaPlayer,FullscreenActivity.this,raws[type]);
                    }
                }

                return false;
            }
        });
    }

    private int yourChoice;
//    private int[] images={R.mipmap.a12,R.mipmap.wenxintishi2,R.mipmap.a11,R.mipmap.a1,R.mipmap.a2_1,
//            R.mipmap.a2_2,R.mipmap.a2_3,R.mipmap.a3,R.mipmap.a1_3,R.mipmap.a1_4,R.mipmap.a1_1,R.mipmap.a1_2};
    private int[] images={R.mipmap.icon1,R.mipmap.icon2,R.mipmap.icon3,R.mipmap.icon4,R.mipmap.icon5,
            R.mipmap.icon6,R.mipmap.icon7,R.mipmap.icon8};
    private int[] raws={R.raw.m1,R.raw.m2,R.raw.m3,R.raw.m4,R.raw.m5,
            R.raw.m6,R.raw.m7,R.raw.m8};
    private void showSingleChoiceDialog(){
//        final String[] items = { "温馨提示1","温馨提示2","党员12月活动站","兴天地",
//                "习语近人","兴灵手巧工坊","一行一意图书馆","新时代红色大讲堂",
//                "党员群众谈心室","锻炼兴体（加活动时间）","锻炼兴体","星罗棋布室"
//        };
        final String[] items = { "党员群众谈心室","锻炼兴体室","新时代红色大讲堂",
                "习语近人","兴灵手巧工坊","兴罗棋布室","兴天地","一兴一意图书馆"
        };
        yourChoice = 0;
        AlertDialog.Builder singleChoiceDialog =
                new AlertDialog.Builder(FullscreenActivity.this);
        singleChoiceDialog.setTitle("请选择活动室类型");
        // 第二个参数是默认选项，此处设置为0
        singleChoiceDialog.setSingleChoiceItems(items, 0,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        yourChoice = which;
                    }
                });
        singleChoiceDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (yourChoice != -1) {
                            type=yourChoice;
                            SharedPreferencesUtils.setInt(FullscreenActivity.this,"type",yourChoice);
//                            if(type<=1){
                                setlistener();
//                            }
                            imageView.setImageResource(images[yourChoice]);
//                            MyUtils.showToast(FullscreenActivity.this,"你选择了" + items[yourChoice]);
                        }
                    }
                });
        singleChoiceDialog.show();
    }
    private void startanim(ImageView imageView, boolean fangxiang,int time){
        if(fangxiang){
            animation = new RotateAnimation(0f, 359f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        }else {
            animation = new RotateAnimation(359f, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        }
        animation.setDuration(time);
        animation.setRepeatCount(-1);//动画的反复次数
        animation.setFillAfter(true);//设置为true，动画转化结束后被应用
        animation.setRepeatMode(Animation.RESTART);
        //让旋转动画一直转，不停顿的重点
        animation.setInterpolator(new LinearInterpolator());
        imageView.startAnimation(animation);//開始动画
    }
}
